package com.gateway.security.common.token;

import com.gateway.common.dto.ApiDefinition;
import com.gateway.plugin.base.condition.data.CookieParameterData;
import com.gateway.plugin.base.condition.data.QueryParameterData;
import org.springframework.http.HttpHeaders;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/12 16:04
 **/
public class QueryServerSecurityTokenRepository implements ServerSecurityTokenRepository {

    private final static String TOKEN_QUERY = HttpHeaders.AUTHORIZATION;

    @Override
    public Mono<SecurityToken> loadToken(ServerWebExchange exchange, ApiDefinition apiDefinition) {
        QueryParameterData parameterData = new QueryParameterData();
        return Mono.fromCallable(() -> {
            String token = parameterData.builder(TOKEN_QUERY, exchange);
            if (!StringUtils.hasText(token)) {
                return null;
            }
            return new DefaultSecurityToken(token, "query", TOKEN_QUERY);
        });
    }
}
